package lucenepdf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.util.PDFTextStripper;


public class Indexer {
   static COSDocument cosDoc = null; 
   static PDDocument pdDoc = null;
   static PDDocument pddDocument = null;
	
	  public  Indexer () {
		  try{
			File indexDir = new File("c:\\indexdir");
			File dataDir = new File("d:\\temp2");
		    
		    
		    long start = new Date().getTime();
		    int numIndexed = index(indexDir, dataDir);
		    long end = new Date().getTime();
		    System.out.println("Se han indizado " + numIndexed + " archivos          en " + (end - start) + " milisegundos");
            
		  }
		  catch(Exception err){
			  System.out.println("Error 1: "+err);
		  }
		    
		  }
		  public static int index(File indexDir, File dataDir)
		    throws IOException {
			  if (!dataDir.exists() || !dataDir.isDirectory()) {
			      throw new IOException(dataDir
			        + " no existe o no es un directorio ");
			    }
			    Directory directory = FSDirectory.open(indexDir);
			    borrarDirectorio(indexDir);
			  IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.LIMITED);
			    writer.setUseCompoundFile(false);
			    indexDirectory(writer, dataDir);
			    int numIndexed = writer.numDocs();
			    writer.optimize();
                directory.close();
                writer.close();
			    return numIndexed;
		  }
		  private static void indexDirectory(IndexWriter writer, File dir)
		    throws IOException {
		    File[] files = dir.listFiles();
		    for (int i = 0; i < files.length; i++) {
		      File f = files[i];
		      if (f.isDirectory()) {
		        indexDirectory(writer, f);  // recurse
		      } else if (f.getName().endsWith(".pdf")) {
		        indexFile(writer, f);
		      }
		    }
		  }
		  
		  private static void borrarDirectorio(File dir){
			  File[] ficheros = dir.listFiles();
			  for (int x=0;x<ficheros.length;x++){
				  if (ficheros[x].isDirectory()) {
					  borrarDirectorio(ficheros[x]);
					}
				  System.out.print("Borrando: "+ficheros[x]);
				  ficheros[x].delete();
			  }
		  }
		  
			private static COSDocument parseDocument(InputStream is)
			throws IOException {
			PDFParser parser = new PDFParser(is);
			parser.parse();
			return parser.getDocument();
			}
		  
		  private static void indexFile(IndexWriter writer, File f)
		    throws IOException {
			  try{
				  if (f.isHidden() || !f.exists() || !f.canRead()) {
				      return;
				    }
				    System.out.println("Indizando " + f.getCanonicalPath());
				    Document doc = new Document();
				    doc.add(new Field("contents", new FileReader(f)));
				    doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED));
				    
				    cosDoc = parseDocument(new FileInputStream(f)); 
				    if (!cosDoc.isEncrypted()){
					    pdDoc = new PDDocument(cosDoc); 
					    PDDocumentInformation docInfo = pdDoc.getDocumentInformation();
					  
					    //contenido
					    
					    pddDocument=PDDocument.load(f);
					    PDFTextStripper textStripper=new PDFTextStripper();
					    String text = textStripper.getText(pddDocument);
					    
					    doc.add(new Field("Contenido",text,Field.Store.YES, Field.Index.ANALYZED));
					    pddDocument.close();
					    //System.out.println(docText);  
					    //System.out.println(text);
					    
		
		//			    doc.add(new Field("Contenido",docInfo.get,Field.Store.YES, Field.Index.NO));
					    
					    if (docInfo.getAuthor() == null){
					       doc.add(new Field("Autor","Sin Autor",Field.Store.YES, Field.Index.ANALYZED));
					    }
					    else{
					    	doc.add(new Field("Autor", docInfo.getAuthor(), Field.Store.YES, Field.Index.ANALYZED));
					    }
					    if (docInfo.getCreationDate().toString() != null){
					        
					    	doc.add(new Field("Fecha",docInfo.getCreationDate().getTime().toLocaleString().substring(0,10),Field.Store.YES, Field.Index.ANALYZED));
					    	System.out.println(docInfo.getCreationDate().getTime().toLocaleString().substring(0,10));
		
					    }
					    else{
					    	doc.add(new Field("Fecha", "Sin fecha", Field.Store.YES, Field.Index.ANALYZED));
					    }
					    
					    doc.add(new Field("Tipo", "pdf", Field.Store.YES, Field.Index.ANALYZED));
					    /*
					    if (docInfo.getTitle() == null ){
					    	doc.add(new Field("Titulo", "Sin titulo", Field.Store.YES, Field.Index.ANALYZED));
					    }
					    else{
					       doc.add(new Field("Titulo", docInfo.getTitle(), Field.Store.YES, Field.Index.ANALYZED));
					    }
					    */
					    cosDoc.close();
					    pdDoc.close();
						    
					    writer.addDocument(doc);
					    writer.optimize();
					    writer.commit();
				    }
				    else{
				    	cosDoc.close();
				    	System.out.println("PDF con password");

				    }
			  }
			    catch(Exception err){
				    cosDoc.close();
				    pdDoc.close();
				    pddDocument.close();			    	
			    	System.out.println("Error!!!: "+err);
			    }

			    //writer.close();			    
//		    System.out.println(docText);
		  }
}
